package com.v.air.app.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.v.air.app.R;
import com.v.air.app.interfaces.UpdateClickListener;
import com.v.air.app.tools.PreferencesTool;
import com.v.air.app.tools.StringUtils;
import com.v.air.app.util.ToastUtil;

public class ConfirmDialog extends Dialog implements
		android.view.View.OnClickListener {

	private UpdateClickListener listener;
	private EditText namEditText;
	private Button okButton;
	private Button cancelButton;

	public ConfirmDialog(Context context, UpdateClickListener listener) {
		super(context, R.style.style_dialog);
		this.listener = listener;
		setContentView(R.layout.update_dialog);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		namEditText = (EditText) findViewById(R.id.name);
		okButton = (Button) findViewById(R.id.ok);
		cancelButton = (Button) findViewById(R.id.cancel);
		namEditText.setFocusable(true);
		namEditText.setFocusableInTouchMode(true);
		namEditText.requestFocus();
		InputMethodManager inputManager = (InputMethodManager) getContext()
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		inputManager.showSoftInput(namEditText, 0);
		String title = PreferencesTool.getString(getContext(), "APP_TITLE");
		if (StringUtils.isEmpty(title)) {
			namEditText.setText(R.string.app_title);
		} else {
			namEditText.setText(title);
		}
		okButton.setOnClickListener(this);
		cancelButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.cancel:
			this.dismiss();
			break;
		case R.id.ok:
			String name = namEditText.getText().toString().trim();
			if (StringUtils.isEmpty(name)) {
				ToastUtil
						.showToast(getContext(), "名称不能为空！", Toast.LENGTH_SHORT);
				return;
			}
			listener.onClick(true, name);
			this.dismiss();
			break;
		default:
			break;
		}
	}
}
